package com.yz.jdd.module.settings;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.module.settings.model.DeviceEntity;
import com.yz.jdd.module.settings.model.PayQrCode;

import java.util.ArrayList;
import java.util.List;

public class SelectDeviceAdapter extends RecyclerView.Adapter<SelectDeviceAdapter.MyViewHolder> {

    private Context context;


    /**
     * 先造一组模拟数据
     */
    private List<DeviceEntity.DeviceList> listData;
    SelectDeviceActivity activity;


    public SelectDeviceAdapter(Context context, List<DeviceEntity.DeviceList> listData) {
        this.context = context;
        this.listData = listData;
        activity = (SelectDeviceActivity) context;

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox select_device_check;
        RelativeLayout rl_device;
        TextView select_device_name, select_device_SN, tv_shop_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            rl_device = itemView.findViewById(R.id.rl_device);
            select_device_check = itemView.findViewById(R.id.select_device_check);
            select_device_name = itemView.findViewById(R.id.select_device_name);
            select_device_SN = itemView.findViewById(R.id.select_device_SN);
            tv_shop_name = itemView.findViewById(R.id.tv_shop_name);
        }
    }

    @NonNull
    @Override
    public SelectDeviceAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_device_select, parent, false);
        SelectDeviceAdapter.MyViewHolder holder = new SelectDeviceAdapter.MyViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) {
        final DeviceEntity.DeviceList deviceEntity = listData.get(position);
        if (TextUtils.isEmpty(deviceEntity.ShopNameLin)) {
            myViewHolder.rl_device.setVisibility(View.VISIBLE);
            myViewHolder.tv_shop_name.setVisibility(View.GONE);
            myViewHolder.select_device_name.setText(machineToolNameByType(deviceEntity.machineToolType));
            myViewHolder.select_device_SN.setText("设备SN:" + deviceEntity.machineToolSn);
            myViewHolder.select_device_check.setChecked(deviceEntity.selected);
            myViewHolder.select_device_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    deviceEntity.selected = isChecked;
                    activity.setSelectSize(listData);
                }
            });
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myViewHolder.select_device_check.performClick();
                }
            });
        } else {
            myViewHolder.rl_device.setVisibility(View.GONE);
            myViewHolder.tv_shop_name.setVisibility(View.VISIBLE);
            myViewHolder.tv_shop_name.setText(deviceEntity.ShopNameLin);
        }

    }

    /**
     * 翻译对应设备名称
     *
     * @param type machineToolType
     * @return
     */
    private String machineToolNameByType(int type) {
        if (type == 1) {
            return "金员宝智能POS";
        } else if (type == 2) {
            return "金员宝智能POS银联版";
        } else if (type == 3) {
            return "扫码设备";
        } else if (type == 4) {
            return "银员宝-云小盒";
        } else if (type == 5) {
            return "小票打印机";
        } else if (type == 6) {
            return "金员宝智慧盒子Wi-Fi版";
        } else if (type == 7) {
            return "金员宝智慧盒子（Wi-Fi+4G）版";
        } else if (type == 8) {
            return "【口碑版】POS基础版（不含卡）";
        } else if (type == 9) {
            return "【口碑版】POS基础版（含卡）";
        } else if (type == 10) {
            return "【口碑版】云小盒PLUS-Wi-Fi版";
        } else if (type == 11) {
            return "【口碑版】云小盒";
        } else if (type == 12) {
            return "【口碑版】小票打印机";
        } else if (type == 13) {
            return "【口碑版】扫码枪";
        } else if (type == 14) {
            return "一年流量卡";
        } else if (type == 15) {
            return "【口碑版】一年流量卡";
        } else if (type == 16) {
            return "【口碑版】云小盒PLUS（Wi-Fi+4G版）";
        } else if (type == 17) {
            return "码先声Ι";
        } else if (type == 18) {
            return "码卡龙";
        } else if (type == 19) {
            return "小程序";
        } else if (type == 20) {
            return "快小盒";
        } else if (type == 32) {
            return "快小盒4G";
        } else if (type == 21) {
            return "码先声Ⅱ";
        }else {
            return "未知机具类型";
        }

    }


    @Override
    public int getItemCount() {
        if (listData == null) {
            return 0;
        }
        return listData.size();
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onViewRecycled(@NonNull MyViewHolder holder) {
        super.onViewRecycled(holder);
    }
}
